package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.ButtonCodeSlice;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.ShapeElement;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;
public class ButtonsSlice extends AbilitySlice {

    private Image back_button, button_image_one, button_image_two,
            button_image_three, button_image_four, button_images_five;
    private Text text_title;
    private Button btn_half, btn_shadow, btn_fold_two, btn_fold;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_buttons);
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Banner")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Banner")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Banner", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Banner", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Banner", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        Element element = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_code_black_24dp);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        button_image_one = (Image) findComponentById(ResourceTable.Id_button_images_one);
        button_image_two = (Image) findComponentById(ResourceTable.Id_button_images_two);
        button_image_three = (Image) findComponentById(ResourceTable.Id_button_images_three);
        button_image_four = (Image) findComponentById(ResourceTable.Id_button_images_four);
        button_images_five = (Image) findComponentById(ResourceTable.Id_button_images_five);
        btn_shadow = (Button) findComponentById(ResourceTable.Id_btn_shadow);
        btn_half = (Button) findComponentById(ResourceTable.Id_btn_half);
        btn_fold = (Button) findComponentById(ResourceTable.Id_btn_fold);
        ShapeElement shapeElement = new ShapeElement(getContext(), ResourceTable.Graphic_btn_shape_half);
        shapeElement.setCornerRadiiArray(new float[]{
                80f, 80f,
                0f, 0f,
                0f, 0f,
                0f, 0f
        });
        btn_half.setBackground(shapeElement);
        button_image_one.setImageElement(element);
        button_image_two.setImageElement(element);
        button_image_three.setImageElement(element);
        button_image_four.setImageElement(element);
        button_images_five.setImageElement(element);
        text_title.setText("Buttons");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());

        button_image_one.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                present(new ButtonCodeSlice(), intent);
            }
        });

        button_image_two.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                present(new ButtonCodeSlice(), intent);
            }
        });

        button_image_three.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                present(new ButtonCodeSlice(), intent);
            }
        });

        button_image_four.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                present(new ButtonCodeSlice(), intent);
            }
        });

        button_images_five.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                present(new ButtonCodeSlice(), intent);
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
